const { sqlite3 } = require('../../mysql')
const tools = require('../../utlis/tools')
const koaReq = require('request')
const koa2Req = require('koa2-request')

// 管理平台获取留言列表数据
const getGuestbook = async (ctx) => {
  // console.log('ctx.query:', ctx.query)
  let formData = ctx.query
  console.log('keyword:', formData.keyword)
  let offset = (formData.page - 1) * formData.limit
  let keyword = formData.keyword
  let limitdata = {}
  let data = {}
  if (keyword) {
    data = await sqlite3('guestbook')
      .where('title', 'LIKE', '%' + keyword + '%')
      .orderBy('id', 'desc')
      .select()
      .limit(formData.limit)
      .offset(offset)
    total = data.length
  } else {
    limitdata = await sqlite3('guestbook').count({ total: '*' })
    data = await sqlite3('guestbook')
      .orderBy('id', 'desc')
      .select()
      .limit(formData.limit)
      .offset(offset)
    total = limitdata[0].total
  }
  if (data) {
    console.log('limitdata:', limitdata)
    console.log('data:', data)
    ctx.body = {
      code: 200,
      item: data,
      total,
      result: true,
    }
  } else {
    ctx.body = {
      code: false,
    }
  }
}

const recordGuestbook = async (ctx) => {
  const res = ctx.request.body
  res.create_time = tools.GMTtoStr(new Date()) // 修改数据库登录时间
  console.log('res:', res)
  const data = await sqlite3('guestbook').insert({
    'open_id': res.open_id,
    'nickName': res.nickName,
    'avatarUrl': res.avatarUrl,
    'type': res.type,
    'content': res.content,
    'show': 1,
    'create_time': res.create_time
  })
  if (data) {
    console.log('data:', data)
    ctx.body = {
      code: 200,
      result: true,
    }
  } else {
    ctx.body = {
      code: false,
    }
  }
}

// 后台request
const getOpenid = async (ctx) => {
  const resCode = ctx.query.rescode
  const apiUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid='
  const appId = 'wx44f41fa28c37d82e'
  const secret = '735494b27b7a08411c7039dd655ddb2b'
  const url = apiUrl + appId + `&secret=` + secret + `&js_code=` + resCode + `&grant_type=authorization_code`
  const res = await koa2Req(url)
  // ctx.body = resCode
  // const res = await koa2Req('http://localhost:8100/api')
  if (res.statusCode === 200) {
    ctx.body = JSON.parse(res.body)
    console.log('ctx.body:', ctx.body)
  } else {
    ctx.body = {
      code: false,
    }
  }
}

// const getOpenid = async function (ctx, next) {
//   var apiUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid='
//   var appId = 'wx44f41fa28c37d82e'
//   var secret = '735494b27b7a08411c7039dd655ddb2b'
//   await koaReq({
//     // uri: 'http://localhost:8100/api',
//     // url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + appId + '&secret=' + secret + '',
//     url: apiUrl + appId + `&secret=` + secret + `&js_code=` + res.code + `&grant_type=authorization_code`,
//     method: 'get',
//     json: true // 设置返回的数据为json
//   }, function (error, response, body) {
//     if (!error && response.statusCode === 200) {
//       console.log(body)
//       const res = body
//       ctx.body = {
//         code: 200,
//         result: true,
//       }
//     } else {
//       ctx.body = {
//         code: false,
//       }
//     }
//   })
//   await next(ctx.body = {
//     item: res,
//     code: 200,
//     result: true,
//   })
// }

// const koa2Req = require('koa2-request')

// // router.post('/login', async (ctx) => {
//   const login = router.post('/login', async (ctx) => {
//   const { code } = ctx.request.body
//   var apiUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid='
//   var appId = 'wx44f41fa28c37d82e'
//   var secret = '735494b27b7a08411c7039dd655ddb2b'
//   const res = await koa2Req({
//     url: apiUrl + appId + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code'
//   })
//   let body = JSON.parse(res.body)
//   ctx.body = { status: body }
// })

module.exports = { getGuestbook, recordGuestbook, getOpenid }
